/**
 * Puzzle8Handler.java
 *
 * Fecha de creación: Mar 12, 2012,10:19:27 PM
 *
 * Autor: José Ignacio Rivero Dávila
 */
package model;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.Movement;
import util.MovementNotAllowedException;
import util.NoMoreMovesException;

public final class Puzzle8RobotModel extends AbstractModel {

    private Puzzle8Model puzzle;
    private Iterator<Movement> moves;
    private Puzzle8SolverModel solver;

    /**
     *
     */
    public Puzzle8RobotModel(Puzzle8Model puzzle, Iterator<Movement> moves, Puzzle8SolverModel solver) {
        this(puzzle,solver);
        this.moves = moves;
    }

    public Puzzle8RobotModel(Puzzle8Model puzzle, Puzzle8SolverModel solver) {
        this.puzzle = puzzle;
        this.solver = solver;
    }

    public void moveRandomly(int numberOfMoves) {
        LinkedList<Movement> list = generateRadomListOfMovements(numberOfMoves);
        for (Movement movement : list) {
            tryMove(movement);
        }
    }
    public void calculateSolution(){
        
        moves = solver.solve(puzzle).iterator();
        
    }

    private LinkedList<Movement> generateRadomListOfMovements(int number) {
        LinkedList<Movement> list = new LinkedList<Movement>();
        Random rnd = new Random();
        int switcher;
        for (int i = 0; i < number; i++) {
            switcher = 1 + rnd.nextInt(4);
            switch (switcher) {
                case 1:
                    list.add(Movement.UP);
                    break;
                case 2:
                    list.add(Movement.RIGHT);
                    break;
                case 3:
                    list.add(Movement.DOWN);
                    break;
                case 4:
                    list.add(Movement.LEFT);
                    break;
            }
        }
        return list;
    }

    @Override
    public String toString() {
        return puzzle.toString();
    }

    public void tryMove(Movement move) {
        try {
            puzzle.move(move);
        } catch (MovementNotAllowedException ex) {
        }
    }

    public boolean hasNextMove() {
        return moves.hasNext();
    }

    public void nextMove() throws NoMoreMovesException{
        if(!hasNextMove())
            throw new NoMoreMovesException();
        try {
            puzzle.move(moves.next());
        } catch (MovementNotAllowedException ex) {
            System.err.println(ex);
        }
    }
    public void autoPlay() throws NoMoreMovesException{
        if(!hasNextMove())
            throw new NoMoreMovesException();
        try{
            while(moves.hasNext()){
                puzzle.move(moves.next());
            }
            
        }catch(MovementNotAllowedException ex){
            System.err.println(ex);
        }
    }


    public static void main(String[] args) {
        Puzzle8RobotModel robot = new Puzzle8RobotModel(null, null);
        System.out.println(robot.toString());
    }
}
